<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
description("Tests RTCPeerConnection onnegotiationneeded.");

var stream = null;
var pc = null;
var onNegotiationNeededCalled = false;

function error() {
    testFailed('Stream generation failed.');
    finishJSTest();
}

function getUserMedia(dictionary, callback) {
    try {
        navigator.webkitGetUserMedia(dictionary, callback, error);
    } catch (e) {
        testFailed('webkitGetUserMedia threw exception :' + e);
        finishJSTest();
    }
}

function onNegotiationNeeded(event) {
    if (onNegotiationNeededCalled) {
        testFailed('onNegotiationNeeded was called multiple times.');
        finishJSTest();
        return;
    }
    testPassed('onNegotiationNeeded was called.');
    onNegotiationNeededCalled = true;

    finishJSTest();
}

function gotStream(s) {
    testPassed('Got a stream.');
    stream = s;

    pc = new RTCPeerConnection();
    pc.onnegotiationneeded = onNegotiationNeeded;

    pc.addStream(stream);
    Promise.resolve()
    .then(() => {
        if (onNegotiationNeededCalled) {
            testFailed('onNegotiationNeeded called too soon.');
            return;
        }
        testPassed('Promise.resolve().then() runs before onNegotiationNeeded');
    });
}

getUserMedia({audio:true, video:true}, gotStream);

window.jsTestIsAsync = true;
window.successfullyParsed = true;
</script>
</body>
</html>
